<?php

namespace app\zc\controller;

use cmf\controller\AdminBaseController;
use app\zc\model\BannerModel;
use app\zc\validate\BannerValidate;
use think\Db;

class BannerController extends AdminBaseController
{
    public function index()
    {
        $keyword = input('get.keyword', '');
        $where = [];
        if ($keyword) {
            $where[] = ['id', 'like', '%' . $keyword . '%'];
        }

        $bannerList = BannerModel::where($where)
            ->order('list_order', 'desc')
            ->paginate(5);
        $page = $bannerList->render();

        $this->assign('bannerList', $bannerList);
        $this->assign('page', $page);
        return $this->fetch();
    }

    public function add()
    {
        return $this->fetch();
    }

    public function addPost()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();

            $validate = new BannerValidate();
            if (!$validate->check($data)) {
                $this->error($validate->getError());
            }

            if (!isset($data['url'])) {
                $data['url'] = '';
            }

            if (!isset($data['image'])) {
                $this->error('请上传图片');
            }

            $banner = new BannerModel();
            $banner->image = $data['image'];
            $banner->url = $data['url'];

            if ($banner->save()) {
                $this->success('添加成功', url('Banner/index'));
            } else {
                $this->error('添加失败');
            }
        }
    }

    public function toggleSwitch()
    {
        $id = $this->request->param('id', 0, 'intval');
        $news = BannerModel::find($id);
        if ($news) {
            $news->switch = $news->switch ? 0 : 1;
            if ($news->save()) {
                return json(['status' => 1, 'msg' => '状态切换成功', 'switch' => $news->switch]);
            } else {
                return json(['status' => 0, 'msg' => '状态切换失败']);
            }
        } else {
            return json(['status' => 0, 'msg' => '新闻不存在']);
        }
    }

    public function edit($id)
    {
        $banner = BannerModel::find($id);
        if (!$banner) {
            $this->error('轮播图不存在');
        }

        $this->assign('banner', $banner);
        return $this->fetch();
    }

    public function editPost()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();

            $validate = new BannerValidate();
            if (!$validate->check($data)) {
                $this->error($validate->getError());
            }

            if (!isset($data['url'])) {
                $data['url'] = '';
            }

            if (!isset($data['image'])) {
                $this->error('请上传图片');
            }

            $banner = BannerModel::find($data['id']);
            if (!$banner) {
                $this->error('轮播图不存在');
            }

            $banner->image = $data['image'];
            $banner->url = $data['url'];

            if ($banner->save()) {
                $this->success('编辑成功', url('Banner/index'));
            } else {
                $this->error('编辑失败');
            }
        }
    }


    public function delete($id)
    {
        $banner = BannerModel::find($id);
        if (!$banner) {
            $this->error('轮播图不存在');
        }

        if ($banner->delete()) {
            $this->success('删除成功');
        } else {
            $this->error('删除失败');
        }
    }

    public function order()
    {
        parent::listOrders(new BannerModel());
        $this->success('排序成功');
    }
}
